﻿/**************************************************************
 * au  :zhuyehong
 * time:2012.8
 GUIDE begin
 **************************************************************/

function GUIDE(myname, buttonID, stepsID) {
  var guide = {};

  guide.myname = '';
  guide.buttonPos = '';
  guide.stepsPos = '';

  guide.steps_selector = 0;
  guide.selector_change = null;
  guide.steps = [];
  guide.button_list = {};
  guide.pipeline = [];

  guide.save_function = null;
  guide.save_param = [];
  guide.submit_function = null;

  guide.back_step_value = [];

  guide.INIT_GUIDE = function (steps) {
    guide.steps = steps;
    guide.button_list = new BUTTON_LIST(guide.myname + '.button_list', //
      guide.buttonPos.substring(1, guide.buttonPos.length));

    var list = [], i = 0;
    for (i = 0; i < steps.length; i += 1) {
      list.push(i + 1);
    }

    guide.button_list.SET_BUTTONS(list, 0, list, guide.steps);
    guide.button_list.allow_manual = false;

    guide.button_list.SHOW_BUTTONS(0);
    guide.button_list.SHOW_TABLE();

    BUTTON_LIST_OPEN_AUTO(guide.button_list, guide.steps_selector);

    guide.CHANGE_STEP_MENU();
  };

  guide.GUIDE_STEP_FORWARD = function () {
    guide.back_step_value = [];

    if (guide.pipeline[guide.pipeline.length - 1] != guide.steps_selector)
      guide.pipeline.push(guide.steps_selector);

    if (typeof guide.selector_change === 'function') {
      guide.steps_selector = guide.selector_change(guide.steps_selector);

      if (guide.steps_selector == null) {
        alert("no Return Value！");
        return;
      }

      if (guide.steps_selector >= guide.steps.length) {
        guide.pipeline.pop();
        alert("Wrong step！");
        return;
      }
    } else {
      guide.steps_selector++;

      if (guide.steps_selector >= guide.steps.length) {
        guide.steps_selector--;
        guide.pipeline.pop();
      }
    }

    if (typeof guide.save_function == 'function') {
      guide.save_function();
    }

    BUTTON_LIST_OPEN_AUTO(guide.button_list, guide.steps_selector);
    guide.CHANGE_STEP_MENU();

  };

  guide.GUIDE_STEP_BACK = function () {
    if (guide.pipeline.length > 0) {
      guide.steps_selector = guide.pipeline.pop();
      guide.back_step_value = guide.save_param.pop();
      BUTTON_LIST_OPEN_AUTO(guide.button_list, guide.steps_selector);
      guide.CHANGE_STEP_MENU();
    }
  };

  guide.GUIDE_SUBMIT = function () {
    if (typeof guide.submit_function === 'function') {
      guide.submit_function();
    } else {
      alert("Please register a submit function first！");
    }
  };

  guide.CHANGE_STEP_MENU = function () {
    var html = "<a class='btn1' onclick='" + guide.myname + ".GUIDE_STEP_BACK()'><span>" + //
      top.T.BTN.PreStep + "</span></a><a class='btn1' onclick='" + guide.myname + //
      ".GUIDE_STEP_FORWARD()'><span>" + top.T.BTN.NextStep + "</span></a>";

    if (guide.steps_selector == 0) {
      html = "<a class='btn1' style='visibility: hidden;' onclick='" + guide.myname + ".GUIDE_STEP_BACK()'><span>" + //
        top.T.BTN.PreStep + "</span></a><a class='btn1' onclick='" + guide.myname + //
        ".GUIDE_STEP_FORWARD()'><span>" + top.T.BTN.NextStep + "</span></a>";
    }

    if (guide.steps_selector == (guide.steps.length - 1)) {
      html = "<a class='btn1' onclick='" + guide.myname + ".GUIDE_STEP_BACK()'><span>" + //
        top.T.BTN.PreStep + "</span></a><a class='btn1' style='visibility: hidden;' onclick='" + guide.myname + //
        ".GUIDE_STEP_FORWARD()'><span>" + top.T.BTN.NextStep + "</span></a>";
    }

    $(guide.stepsPos).empty();
    $(guide.stepsPos).append(html);
  };

  guide.INIT = function () {
    guide.myname = myname;
    guide.buttonPos = "#" + buttonID;
    guide.stepsPos = "#" + stepsID;
  };

  guide.INIT();
  return guide;
}

/**************************************************************
 * au  :zhuyehong
 * time:2012.7
 *BUTTON_LIST begin
 **************************************************************/
function BUTTON_LIST(myname, menuID) {
  var btns = {};
  btns.title = [];
  btns.actions = [];
  btns.values = [];
  btns.buttons_selector = 0;
  btns.buttons_html = '';
  btns.myname = '';
  btns.menuPos = '';
  btns.allow_manual = true;

  btns.SET_BUTTONS = function (title, selector, values, action) {
    selector = selector ? selector : 0;
    btns.buttons_selector = selector;
    btns.title = title;
    btns.actions = action;
    btns.values = values;
    var html = '', i = 0, len = 0;

    for (i = 0, len = btns.title.length; i < len; i++) {
      if (i == btns.buttons_selector) {
        html = html + "<span class='current' id='" + btns.myname + "_selected" + //
          "' onclick='BUTTON_LIST_OPEN(" + btns.myname + ',' + i + ")'>" + btns.title[i] + "</span> ";
      } else {
        if (!btns.allow_manual) {
          html = html + "<a style=''  onclick='BUTTON_LIST_OPEN(" + btns.myname + "," + i + ")'>" + //
            btns.title[i] + "</a>";
        } else {
          html = html + "<a style='cursor:pointer;'  onclick='BUTTON_LIST_OPEN(" + //
            btns.myname + "," + i + ")'>" + btns.title[i] + "</a>";
        }
      }
    }

    btns.buttons_html = html;
  };

  btns.SHOW_BUTTONS = function (i) {
    btns.SET_BUTTONS(btns.title, i, btns.values, btns.actions);
    btns.tabs_selector = i;
    $(btns.menuPos).empty();
    $(btns.menuPos).append(btns.buttons_html);
  };

  btns.SHOW_TABLE = function () {
    var id = "#" + btns.myname + "_selected";
    $(id).click();
  };

  btns.INIT = function () {
    btns.myname = myname;
    btns.menuPos = "#" + menuID;
  };

  btns.INIT();
  return btns;
}

function BUTTON_LIST_OPEN_AUTO(myname, tabId) {
  myname.SHOW_BUTTONS(tabId);
  if (typeof myname.actions[tabId] === 'function') {
    myname.actions[tabId](myname.values[tabId]);
  } else {
    try {
      myname.actions(myname.values[tabId]);
    } catch (e) {
      alert("Title callback need a function!");
    }
  }
}

function BUTTON_LIST_OPEN(myname, tabId) {
  if (!myname.allow_manual) {
    return;
  }

  myname.SHOW_BUTTONS(tabId);
  if (typeof myname.actions[tabId] === 'function') {
    myname.actions[tabId](myname.values[tabId]);
  } else {
    try {
      myname.actions(myname.values[tabId]);
    } catch (e) {
      alert("Title callback need a function!");
    }
  }
}